package com.sea.human.controller;

import com.codecrane.core.Constants;
import com.codecrane.core.power.entity.PowerGroup;
import com.codecrane.core.power.entity.PowerUser;
import com.codecrane.core.power.service.PowerGroupService;
import com.codecrane.core.power.service.PowerUserService;
import com.codecrane.core.util.DateUtils;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.human.entity.Performance;
import com.sea.human.entity.PerformanceCheck;
import com.sea.human.service.PerformanceCheckService;
import com.sea.human.service.PerformanceService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.Perf;

import java.util.List;


/**
 *  控制器
 * @author 陈鸿飞
 *  <br/>
 *  <br/>
 */
@Controller
@RequestMapping("/sea/human/performanceGrade")
public class PerformanceGradeController {

    @Autowired
    private PerformanceService performanceService;
    @Autowired
    private PowerGroupService powerGroupService;
    @Autowired
    private PowerUserService powerUserService;



    /**
     * 管理主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        model.addAttribute("powerGroup",powerGroupService.findByCnd(null));
        return "/sea/human/performanceGrade/main";
    }

    /**
     * 新增主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/grade")
    public String grade(Model model,@RequestParam("id")Integer id,@RequestParam(value = "type",required = false)Integer type) {
        PowerUser user = (PowerUser) SecurityUtils.getSubject().getSession().getAttribute(Constants.CURRENT_USER_SESSION);
        PowerGroup powerGroup = powerGroupService.findGroupByUserId(Integer.parseInt(user.getId().toString()));
        model.addAttribute("user",user);
        model.addAttribute("powerGroup",powerGroup);
        model.addAttribute("performanceDate", DateUtils.getCurrentDateTime("yyyy-MM"));
        model.addAttribute("performanceId",id);
        model.addAttribute("performance",performanceService.findById(id));
        //人事界面进入
        model.addAttribute("type",type);
        return "/sea/human/performanceGrade/detail";
    }

    @RequestMapping("/gradeSelf")
    public String gradeSelf(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("id",id);
        return "/sea/human/performance/gradeSelf";
    }


    /**
     * 新增主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(Model model) {
        List<PowerGroup> powerGroup = powerGroupService.findAllPowerGroup();
        model.addAttribute("powerGroup",powerGroup);
        List<PowerUser> allPowerUser = powerUserService.findAllPowerUser();
        model.addAttribute("PowerUser",allPowerUser);
        return "/sea/human/performanceCheck/add";
    }
    /**
     * 新增主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model,@RequestParam("id")Integer id) {
        List<PowerGroup> powerGroup = powerGroupService.findAllPowerGroup();
        model.addAttribute("powerGroup",powerGroup);
        List<PowerUser> allPowerUser = powerUserService.findAllPowerUser();
        model.addAttribute("PowerUser",allPowerUser);
        return "/sea/human/performanceCheck/edit";
    }
    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByGrade")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     Performance performance) {
        PowerUser user = (PowerUser) SecurityUtils.getSubject().getSession().getAttribute(Constants.CURRENT_USER_SESSION);
        performance.setPerformanceCheck(user.getUserRealname());
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<Performance> performanceList = performanceService.findGradeByUser(performance);
        PageInfo<Performance> page = new PageInfo<>(performanceList);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }



}
